using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;


namespace WindowsApplication4
{
    public partial class Diagnostics : Form
    {

        DataTable dtPlayers;
        DataTable dtVillages;
        DataSet worldData;

        public Diagnostics()
        {
            InitializeComponent();


            dtPlayers = new DataTable("Players");
            dtPlayers.Columns.Add("ID", System.Type.GetType("System.Int32"));
            dtPlayers.Columns.Add("name", System.Type.GetType("System.String"));
            dtPlayers.Columns.Add("ally", System.Type.GetType("System.Int32"));
            dtPlayers.Columns.Add("villages", System.Type.GetType("System.Int32"));
            dtPlayers.Columns.Add("points", System.Type.GetType("System.Int32"));
            dtPlayers.Columns.Add("rank", System.Type.GetType("System.Int32"));

            dtVillages = new DataTable("Villages");
            dtVillages.Columns.Add("ID", System.Type.GetType("System.Int32"));
            dtVillages.Columns.Add("name", System.Type.GetType("System.String"));
            dtVillages.Columns.Add("X", System.Type.GetType("System.Int32"));
            dtVillages.Columns.Add("y", System.Type.GetType("System.Int32"));
            dtVillages.Columns.Add("tribe", System.Type.GetType("System.Int32"));
            dtVillages.Columns.Add("points", System.Type.GetType("System.Int32"));
            dtVillages.Columns.Add("rank", System.Type.GetType("System.Int32"));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StreamReader objReader;

            objReader = new StreamReader(@"D:\data\855720322\My Documents\Personal\w3tribe.txt");


            string line;
            string[] splitLine;
            while ((line = objReader.ReadLine()) != null)
            {
                //                listBox1.Items.Add(line);
                splitLine = line.Split(',');
                splitLine[1] = System.Web.HttpUtility.UrlDecode(splitLine[1]);

                dtPlayers.Rows.Add(splitLine);

            }
            dtPlayers.Rows.Add(new object[] { 0, "", 0, 0, 0, 0 });

            objReader.Close();




            objReader = new StreamReader(@"D:\data\855720322\My Documents\Personal\w3villages.txt");


            while ((line = objReader.ReadLine()) != null)
            {
                ///                listBox1.Items.Add(line);
                splitLine = line.Split(',');
                splitLine[1] = System.Web.HttpUtility.UrlDecode(splitLine[1]);

                dtVillages.Rows.Add(splitLine);

            }

            objReader.Close();

            worldData = new DataSet();
            worldData.Tables.Add(dtPlayers);
            worldData.Tables.Add(dtVillages);
            //worldData.Relations.Add(dtPlayers.Columns[0], dtVillages.Columns[5]);


            //dataGrid.DataSource = worldData;
            //dataGrid.DataMember = "Villages";

            worldData.WriteXml(@"D:\data\855720322\My Documents\Personal\w3.xml", XmlWriteMode.WriteSchema);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            worldData = new DataSet();
            worldData.Tables.Add(dtPlayers);
            worldData.Tables.Add(dtVillages);

            this.Cursor = Cursors.WaitCursor;
            worldData.ReadXml(@"D:\data\855720322\My Documents\Personal\w3.xml", XmlReadMode.ReadSchema);
            this.Cursor = Cursors.Default;

            worldData.Relations.Add(dtPlayers.Columns[0], dtVillages.Columns[4]);


            dataGrid.DataSource = worldData;
            dataGrid.DataMember = "Villages";


            dataGrid2.DataSource = worldData; 

        }
    }
}